<template>
  <div class="hp100 overflow-hidden">
    <!-- <pre>{{ formatParameter }}</pre> -->
    <!-- <pre>{{ tempData }}</pre> -->
    <!-- {{ msgList }} -->
    <WxMsg :msgList="msgList" ref="msgBox"></WxMsg>
    <el-pagination
      class="fixed-b"
      @size-change="
        handleSizeChange($event, () => {
          ajaxData(true);
        })
      "
      @current-change="
        handleCurrentChange($event, () => {
          ajaxData();
        })
      "
      :current-page="page.current"
      :page-sizes="page.sizes"
      :page-size="page.size"
      :layout="'total,prev,pager,next'"
      :total="page.total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { bus } from '@/main.js';
import { mapState, mapMutations } from 'vuex';
import pagination from '@/mixin/pagination.vue';
import tableList from '@/mixin/tableList.vue';
import aboutChat from '@/mixin/aboutChat.vue';
export default {
  name: 'theSessionFile', //会话存档
  mixins: [tableList, pagination, aboutChat],
  components: {
    WxMsg: () => {
      return import('@/components/wx_messageArea.vue');
    },
  },
  data() {
    return {
      isDev: process.env.NODE_ENV == 'development',
      visible: false,
      tempData: null,
      msgList: [],
      page: {
        size: 100,
      },
    };
  },
  computed: {
    ...mapState(['userInfo', 'conditional', 'drawer']),
  },
  methods: {
    handleOpen: function() {
      this.page.current = 1;
    },
    handleClose: function() {
      this.tempData = null;
      this.msgList.splice(0, this.msgList.length);
    },
    ajaxData: function(is2P1) {
      if (is2P1) this.page.current = 1;
      let row = this.tempData;
      let params = {
        wxId: this._.get(row, 'eS_Company_Id', ''), //企业微信id
        sendId: this._.get(row, 'external_userid', ''), //发送用户id
        receiveId: this._.get(row, 'external_userid', ''), // String	否	接收用户id
        startTime: this.$moment(this._.get(row, 'addTime', '')).valueOf(), //Long	否	开始时间 ms单位（13位的时间戳）
        endTime: this.$moment().valueOf(), //Long	否	结束时间 ms单位（13位的时间戳）
        // nowTime: Long	是	当前时间10位的时间戳，以秒为单位，验证消息的时效性：当前时间的前后10分钟有效
        // sign: String	是	加密后的MD5值,（key值 + nowTime）MD5
        from: this.page.current, //Integer	是	分页参数，起始位置
        size: this.page.size, //String	是	分页参数，获取条数
        sort: 1, //1:时间正序，2：时间倒序
      };
      this.$api.会话存档同步
        .获取所有会话({}, params)
        .then(res => {
          if (res.code == 8200) {
            return Promise.resolve(res);
          } else {
            return Promise.reject(res);
          }
        })
        .then(res => {
          this.page.total = res.result.total;
          this.msgList.splice(0, this.msgList.length);

          res.result.message.forEach(item => {
            this.msgList.push(this.format(item));
          });
          return Promise.resolve({ code: 200 });
        })
        .then(res => {
          this.$refs.msgBox.scrollTop();
        })
        .catch(err => {
          console.error(err);
        })
        .finally(_ => {});
    },
  },
  mounted: function() {
    let eS_Company_Id = this._.get(this.$route.query, 'eS_Company_Id', ''); //企业微信id
    let external_userid = this._.get(this.$route.query, 'external_userid', ''); //发送用户id
    let addTime = this.$moment(
      this._.get(this.$route.query, 'addTime', ''),
    ).valueOf(); //Long	否	开始时间 ms单位（13位的时间戳）
    this.tempData = {
      eS_Company_Id,
      external_userid,
      addTime,
    };
    this.$nextTick(() => {
      this.ajaxData(true);
    });
    // bus.$on('调用会话存档', e => {
    // this.init(e);
    // });
  },
};
</script>

<style scoped lang="less">
.fixed-b {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}
</style>
